package servlet.user;

import bean.dto.Login;
import bean.pojo.User;
import bean.vo.UserVo;
import com.alibaba.fastjson.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.r.R;
import service.LogService;
import service.UsersService;
import service.impl.LogServiceImpl;
import service.impl.UsersServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户登录
 */
@WebServlet(name = "RegisterServlet", value ="/register/*")
public class RegisterServlet extends BaseServlet {
    /**
     * 查询用户是否存在,存在返回false
     * @param request
     * @return
     */
    public R tfExistUserName(HttpServletRequest request){

        String  userName =request.getParameter("userName");
        System.out.println("userName:"+userName);
        UsersService usersService = new UsersServiceImpl();
        Boolean valid =usersService.tfExistUserName(userName);
        R  r= R.ok().addData("valid",valid);
        return r;
    }
    public R registerUser(HttpServletRequest request) throws IOException {
        BufferedReader reader=request.getReader();
        //数据
        String  data="";
        //接收数据
        data= reader.readLine();//"{'username':'1','passwor':'1'.....}"
        //把数据格式化(解析)
        User user = JSONObject.parseObject(data, User.class);
        System.out.println("registerUser:user:"+user);
        UsersService usersService = new UsersServiceImpl();
        Integer userId=usersService.registerUser(user);
        if(userId>0){
            LogService logService = new LogServiceImpl();
            logService.insert(userId,user.getUserName(),"注册", LocalDateTime.now());
            return R.ok();
        }
        return R.error();
    }


}
